<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/16 0016
 * Time: 上午 11:08
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Safety extends BasicAdmin
{
    public $table = 'Safety';

    public function index(){
        $this->title = '分栏';
        $db = Db::name($this->table)->where(['is_deleted'=>1])->order('sort asc,id asc');
        return parent::_list($db,false);
    }

    public function add(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            DataService::save($this->table, $post);
            $this->success('保存成功', '');
        }
        $id = $this->request->get('id');
        $db = Db::name($this->table)->where('id',$id)->find();
        if (!empty($db)){
            $title = $db['title'];
        }else{
            $title = '添加';
        }
        $this->assign([
            'title'=>$title,
            'vo'=>$db
        ]);
        return view();
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}